//
//  PickerView.swift
//  SwiftProject
//
//  Created by qindong on 2021/4/13.
//

import UIKit

class PickerView: UIView, UIPickerViewDataSource, UIPickerViewDelegate {
    
    let pickerV = UIPickerView()
    
    var dataSource : [String] = [] {
        didSet {
            pickerV.reloadAllComponents()
        }
    }
    
    private var dictionaryArray : [[String : String]]?
    var selectDictionaryBlock : ((_ dictionary : [String : String]?)->Void)?
    var selectBlock : ((_ date : String?)->Void)?

    init() {
        super.init(frame: .zero)
    }
    
    private override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 229))
        
        backgroundColor = .white
        
        let sureBtn = QMUIButton()
        sureBtn.setTitle("确定", for: .normal)
        sureBtn.setTitleColor(.colorMain, for: .normal)
        sureBtn.titleLabel?.font = .systemFont(ofSize: 18)
        sureBtn.tag = 1
        sureBtn.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
        addSubview(sureBtn)
        
        sureBtn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 62, height: 42))
            make.right.top.equalTo(0)
        }
        
        let cancalBtn = QMUIButton()
        cancalBtn.setTitle("取消", for: .normal)
        cancalBtn.setTitleColor(.colorMake(153, 153, 153), for: .normal)
        cancalBtn.titleLabel?.font = .systemFont(ofSize: 18)
        cancalBtn.tag = 2
        cancalBtn.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
        addSubview(cancalBtn)
        
        cancalBtn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 62, height: 42))
            make.left.top.equalTo(0)
        }
        
        pickerV.frame = CGRect(x: 0, y: 42, width: SCREEN_WIDTH, height: 187)
        pickerV.backgroundColor = .white
        pickerV.dataSource = self
        pickerV.delegate = self
        addSubview(pickerV)
    }
    
    func setDataSourceWithDictionaryArray(_ dictionaryArray : [[String : String]], _ key : String) {
        self.dictionaryArray = dictionaryArray
        dataSource = self.dictionaryArray!.map{
            $0[key]!
        }
        pickerV.reloadAllComponents()
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        dataSource.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        dataSource[row]
    }
    
    @objc func buttonClick(_ button : QMUIButton) {
        switch button.tag {
        case 1:
            if dictionaryArray != nil {
                if selectDictionaryBlock != nil {
                    selectDictionaryBlock!(dictionaryArray![pickerV.selectedRow(inComponent: 0)])
                }
            } else {
                if selectBlock != nil {
                    selectBlock!(dataSource[pickerV.selectedRow(inComponent: 0)])
                }
            }
            break
        default:
            if dictionaryArray != nil {
                if selectDictionaryBlock != nil {
                    selectDictionaryBlock!(nil)
                }
            } else {
                if selectBlock != nil {
                    selectBlock!(nil)
                }
            }
            break
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
